using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;


namespace AzureConfigTask.Features.Azure_Task_Job
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("22780f01-41c2-48d0-978f-0fe7f3ab0c60")]
    public class Azure_Task_JobEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                //add the job definition during installation

                //get the web app so we can work with the job definitions collection
                SPWebApplication wa = (SPWebApplication)properties.Feature.Parent;

                //first enumerate jobs on the web app so that if it already exists we can                   
                //delete it; otherwise it will throw an error
                DeleteJob(wa);

                //create the job; this makes it available to run in CA and PS
                //need to add a schedule too in order to get it to show up in the UI
                AzureTask at = new AzureTask(wa);
                SPYearlySchedule sch = new SPYearlySchedule();
                sch.NextOccurrence(DateTime.Now.AddYears(1));
                at.Schedule = sch;
                at.Title = AzureTask.JobName;
                at.Update();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw ex;
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                //remove the job definition when uninstalling feature
                SPWebApplication wa = (SPWebApplication)properties.Feature.Parent;
                DeleteJob(wa);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw ex;
            }
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        private bool DeleteJob(SPWebApplication wa)
        {
            bool success = true;

            try
            {
                //enumerate jobs on the web app and if it already exists delete it                  
                foreach (SPJobDefinition jDef in wa.JobDefinitions)
                {
                    if (jDef.DisplayName == AzureTask.JobName)
                    {
                        jDef.Delete();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                success = false;
            }

            return success;
        }

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
